package com.hhs.storage.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hhs.storage.bean.*;
import com.hhs.storage.service.*;
import com.hhs.storage.util.StringUtil;
import com.hhs.storage.util.SysUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chx
 * @since 2024-10-20
 */
@RestController
@RequestMapping("xmwork")
@Slf4j
public class XmWorkController {
    @Resource
    private XmWorkService xmWorkService;
    @Resource
    private HttpServletRequest request;
    @Resource
    private BaseUserService baseUserService;
    @Resource
    private XmSchoolService xmSchoolService;
    @Resource
    private XmUserWorkService xmUserWorkService;
    @Value("${upload.folderName}")
    private String folderName;

    @RequestMapping("edit")
    public Map<String,Object> edit(XmWork xmWork){
        BaseUser byId = baseUserService.getById(SysUtil.getLoginUserId(request));
        if(StringUtil.iszNull(xmWork.getId())){
            if(!StringUtil.iszNull(byId.getSchoolId())){
                xmWork.setSchoolId(byId.getSchoolId());
            }
            xmWork.setCreateTime(LocalDateTime.now());
        }
        xmWorkService.saveOrUpdate(xmWork);
        return ResponseInfo.success();
    }
    @RequestMapping("table")
    public Map<String,Object> table(InputParam inputParam){
        Map<String,Object> map=new HashMap<>();
        //得到登录者所在的学校
        BaseUser byIdUser = baseUserService.getById(SysUtil.getLoginUserId(request));
        QueryWrapper qw=new QueryWrapper();
        if(!"2".equals(byIdUser.getId())) {
            qw.eq("school_id", byIdUser.getSchoolId());
        }
        inputParam.mapParamsNoTail("name");
        if(!StringUtil.iszNull(inputParam.getName())){
            qw.like("work_name", inputParam.getName());
        }
        if(!StringUtil.iszNull(inputParam.getOrderBy())){
            if(inputParam.getAsc()==true){
                qw.orderByAsc(inputParam.getOrderBy());
            }else {
                qw.orderByDesc(inputParam.getOrderBy());
            }
        }
        Page<XmWork> mp = xmWorkService.page(new Page(inputParam.getPage(), inputParam.getSize()),qw);
        //学校名处理
        for (XmWork xmw:mp.getRecords()){
            XmSchool byId = xmSchoolService.getById(xmw.getSchoolId());
            if(byId != null){
                xmw.setSchoolName(byId.getName());
            }
            QueryWrapper xqw=new QueryWrapper();
            xqw.eq("work_id",xmw.getId());
            xqw.inSql("user_id","SELECT a.user_id_ FROM base_role_user a LEFT JOIN base_role b ON a.`role_id_`=b.`id_` WHERE flag_='teacher'");
            int count = xmUserWorkService.count(xqw);
            xmw.setNowNumber(count);
        }
        map.put("code",1);
        map.put("data",mp.getRecords());
        map.put("total",mp.getTotal());
        return map;
    }
    @RequestMapping("dele")
    public Map<String,Object> dele(XmWork xmWork){
        if(StringUtil.iszNull(xmWork.getId())){
            return ResponseInfo.error("没有id,参数错误");
        }
        xmWorkService.removeById(xmWork.getId());
        return ResponseInfo.success();
    }
    @RequestMapping("addTeacher")
    public Map<String,Object> addTeacher(InputParam inputParam){
        if(StringUtil.iszNull(inputParam.getId())){
            return ResponseInfo.error("没有id,参数错误");
        }
        if(StringUtil.iszNull(inputParam.getIds())){
            return ResponseInfo.error("没有ids,参数错误");
        }
        return xmWorkService.addTeacher(inputParam);
    }

    @RequestMapping("cancelTeacher")
    public Map<String,Object> cancelTeacher(InputParam inputParam){
        if(StringUtil.iszNull(inputParam.getId())){
            return ResponseInfo.error("没有id,参数错误");
        }
        if(StringUtil.iszNull(inputParam.getIds())){
            return ResponseInfo.error("没有ids,参数错误");
        }
        return xmWorkService.cancelTeacher(inputParam);
    }

    @RequestMapping("bindWork")
    public Map<String,Object> bindWork(String userId,String workIds){
        if(StringUtil.iszNull(userId)){
            return ResponseInfo.error("没有useid,参数错误");
        }
        QueryWrapper qw=new QueryWrapper();
        qw.eq("user_id",userId);
        if(StringUtil.iszNull(workIds)){
            xmUserWorkService.remove(qw);
        }else{
            String[] ids=workIds.split(",");
            xmUserWorkService.remove(qw);
            for(String id:ids){
                XmUserWork xmUserWork = new XmUserWork();
                xmUserWork.setUserId(userId);
                xmUserWork.setWorkId(id);
                xmUserWorkService.save(xmUserWork);
            }
        }
        return ResponseInfo.success();
    }

    @RequestMapping("getMyWork")
    public Map<String,Object> getMyWork(InputParam inputParam){
        if(StringUtil.iszNull(inputParam.getUserId())){
            return ResponseInfo.error("没有useid,参数错误");
        }
        QueryWrapper qw=new QueryWrapper();
        qw.eq("user_id",inputParam.getUserId());
        List<XmUserWork> list = xmUserWorkService.list(qw);
        for(XmUserWork xmUserWork:list){
            XmWork byId = xmWorkService.getById(xmUserWork.getWorkId());
            xmUserWork.setWork(byId);
            //查询教研室老师数
            InputParam myInputParam=new InputParam();
            myInputParam.setTargetId(byId.getId());
            myInputParam.setRelateType("teacher");
            int count = xmUserWorkService.getUserCount(myInputParam);
            xmUserWork.setUserNumber(count);
        }
        return ResponseInfo.success(list);
    }
}

